package com.gmzg.gmzgnews;

import android.os.Build;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RadioGroup;

import com.gmzg.gmzgnews.me.view.MeFragment;
import com.gmzg.gmzgnews.news.view.NewsFragment;
import com.gmzg.gmzgnews.topic.view.TopicFragment;
import com.gmzg.gmzgnews.live.view.LiveFragment;
import com.readystatesoftware.systembartint.SystemBarTintManager;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {

    private NewsFragment mNewsFragment;
    private TopicFragment mTopicFragment;
    private LiveFragment mLiveFragment;
    private MeFragment mMeFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //注意设置状态栏要写在setContentView(R.layout.activity_main);之前
        //设置状态栏为透明的
        final int sdk = Build.VERSION.SDK_INT;
        Window window = getWindow();
        WindowManager.LayoutParams params = window.getAttributes();

        if (sdk >= Build.VERSION_CODES.KITKAT) {
            final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
            // 设置透明状态栏
            if ((params.flags & bits) == 0) {
                params.flags |= bits;
                window.setAttributes(params);

            }
            SystemBarTintManager tintManager = new SystemBarTintManager(this);
            tintManager.setStatusBarTintEnabled(true);
            tintManager.setStatusBarTintResource(R.color.red);
        }


        setContentView(R.layout.activity_main);


        //管理Fragment,检查Fragment是否存在
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction ts = manager.beginTransaction();
        Fragment f = manager.findFragmentByTag("news");
        //为RadioGroup设置监听事件
        RadioGroup tabBar = (RadioGroup) findViewById(R.id.tab_bar);
        if (tabBar != null) {
            tabBar.check(R.id.rbtn_news);
            tabBar.setOnCheckedChangeListener(this);
        }

        if (f == null) {
            mNewsFragment = new NewsFragment();
            ts.replace(R.id.fragment_container, mNewsFragment, "news");
            ts.commit();
        } else {
            mNewsFragment = (NewsFragment) f;
            ts.replace(R.id.fragment_container, mNewsFragment, "news");
            ts.commit();
        }
        f = manager.findFragmentByTag("topic");
        if (f == null) {
            mTopicFragment = new TopicFragment();
        } else {
            mTopicFragment = (TopicFragment) f;
        }
        f = manager.findFragmentByTag("live");
        if (f == null) {
            mLiveFragment = new LiveFragment();
        } else {
            mLiveFragment = (LiveFragment) f;
        }
        f = manager.findFragmentByTag("me");
        if (f == null) {
            mMeFragment = new MeFragment();
        } else {
            mMeFragment = (MeFragment) f;
        }

    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        //开启一个事务
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction ts = manager.beginTransaction();
        switch (checkedId) {
            case R.id.rbtn_news:
                ts.replace(R.id.fragment_container, mNewsFragment, "news");
                break;
            case R.id.rbtn_live:
                ts.replace(R.id.fragment_container, mLiveFragment, "live");
                break;
            case R.id.rbtn_topic:
                ts.replace(R.id.fragment_container, mTopicFragment, "topic");
                break;
            case R.id.rbtn_me:
                ts.replace(R.id.fragment_container, mMeFragment, "me");
                break;
        }
        //提交事务
        ts.commit();

    }
}
